package Tree;

import Link.ListNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class leet_104 {
}

class Solution_104 {
    public int maxDepth(TreeNode root) {
        int maxDeep = BFS(root);
        return maxDeep;
    }

    public int BFS(TreeNode root){
        int maxDeepth = 0;
        if(root==null){
            return maxDeepth;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        if(queue.offer(root));
        while(!queue.isEmpty()){
            // 开始层级遍历时首先把深度加1
            maxDeepth = maxDeepth + 1;
            int size = queue.size();
            for(int i=0; i<size; i++){
                TreeNode treeNode = queue.poll();
                if(treeNode.left!=null){
                    queue.offer(treeNode.left);
                }

                if(treeNode.right!=null){
                    queue.offer(treeNode.right);
                }
            }
        }

        return maxDeepth;
    }

}
